package com.song.tradition.page.components

import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.compose.foundation.layout.height
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import com.song.tradition.R
import com.song.tradition.ui.theme.customBgColor
import com.song.tradition.ui.theme.replyLightSecondaryContainer

sealed class BottomNavRoute(
    val route: String,
    @StringRes var id: Int,
    @DrawableRes var resId: Int
) {


    object Book :
        BottomNavRoute(
            ScreenRoute.BookScreen.route,
            R.string.category,
            resId = R.drawable.baseline_library_books_24
        )

    object Home :
        BottomNavRoute(ScreenRoute.HomeScreen.route, R.string.home, R.drawable.baseline_home_24)

    object History : BottomNavRoute(
        ScreenRoute.HistoryScreen.route,
        R.string.profile,
        R.drawable.twotone_history_edu_24
    )
}


@Composable
fun BottomNavBarView(
    navController: NavHostController
) {
    val bottomNavList = listOf(
        BottomNavRoute.Home,
        BottomNavRoute.Book,
        BottomNavRoute.History,
    )



    NavigationBar(
        modifier = Modifier
            .height(58.dp), containerColor = customBgColor
    ) {
//        val navBackStackEntry by navController.currentBackStackEntryAsState()
        val navBackStackEntry by navController.currentBackStackEntryAsState()
        val currentDestination = navBackStackEntry?.destination

        bottomNavList.forEach { data ->

            NavigationBarItem(
                icon = {
                    Icon(painterResource(data.resId), contentDescription = data.route)
                },

                selected = currentDestination?.hierarchy?.any { it.route == data.route } == true,
                onClick = {

                    println("BottomNavView当前路由 ===> ${currentDestination?.hierarchy?.toList()}")
                    println("当前路由栈 ===> ${navController.graph.nodes}")
                    if (currentDestination?.route != data.route) {
                        navController.navigate(data.route) {
                            popUpTo(navController.graph.findStartDestination().id) {
                                saveState = true
                            }
                            launchSingleTop = true
                            restoreState = true
                        }
                    }
                },
                colors = NavigationBarItemDefaults.colors(
                    indicatorColor = replyLightSecondaryContainer
                )


            )

        }

    }
}


sealed class ScreenRoute(val route: String) {
    object HomeScreen : ScreenRoute("home_page")
    object BookScreen : ScreenRoute("book_page")
    object SearchScreen : ScreenRoute("search_page")
    object GrassDetailScreen : ScreenRoute("grass_detail_page")
    object PersonDetailScreen : ScreenRoute("person_detail_page")
    object BookDetailScreen : ScreenRoute("book_detail_page")
    object StoryDetailScreen : ScreenRoute("story_detail_page")
    object HistoryScreen : ScreenRoute("history_page")
    object AboutScreen : ScreenRoute("about_page")
    object OriginScreen : ScreenRoute("origin_page")

}

